package service

import (
	"context"
	"database/sql"
	"template/pkg/global"
)

type IUserWalletService interface {
	AddWithdraw(ctx context.Context, address, amount string) error
}

type userWalletService struct{}

func NewUserWalletService() IUserWalletService {
	return &userWalletService{}
}

func (u *userWalletService) AddWithdraw(ctx context.Context, address, amount string) error {
	var (
		err       error
		sqlResult sql.Result
		num       int64
	)
	sqlResult, err = global.GFDB.Ctx(ctx).Model("haiXin_wallet_user").
		Data("withdrawal_num=withdrawal_num - ?", amount).
		Where("address=? and withdrawal_num - ? >= 0", address, amount).Update()

	if err != nil {
		return err
	}

	num, err = sqlResult.RowsAffected()
	if err != nil {
		return err
	}
	if num == 0 {
		return err
	}
	return err
}
